package org.example.leetCode;

/**
 * @author Administrator
 */
public class IsSubStructure {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) {
            return false;
        }
        if (A.val == B.val && dfs(A, B)) {
            return true;
        }

        return isSubStructure(A.left, B) || isSubStructure(A.right, B);

    }

    private boolean dfs(TreeNode a, TreeNode b) {
        if (a == null || b == null) {
            return b == null;
        }
        if (a.val != b.val) {
            return false;
        }

        return dfs(a.left, b.left) && dfs(a.right, b.right);
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
